package com.foreknow.elm.controller;

import com.foreknow.elm.po.DeliveryAddress;
import com.foreknow.elm.service.BusinessService;
import com.foreknow.elm.service.DeliveryAddressService;
import com.foreknow.elm.service.impl.BusinessServiceImpl;
import com.foreknow.elm.service.impl.DeliveryAddressServiceImpl;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

public class DeliveryAddressController {
    /**
     * 根据用户编号查询所属送货地址
     * @param request
     * @return
     */
    public List<DeliveryAddress> listDeliveryAddressByUserId(HttpServletRequest request) {
        DeliveryAddressService service = new DeliveryAddressServiceImpl();
        String userId = request.getParameter("userId");
        return service.listDeliveryAddressByUserId(userId);
    }

    /**
     * 根据送货地址编号查询送货地址
     * @param request
     * @return
     */
    public DeliveryAddress getDeliveryAddressById(HttpServletRequest request){
        DeliveryAddressServiceImpl deliveryAddressService = new DeliveryAddressServiceImpl();
        int daId = Integer.parseInt(request.getParameter("daId"));
        return deliveryAddressService.getDeliveryAddressById(daId);
    }

    /**
     * 向送货地址表中添加一条记录
     * @param request
     * @return
     */
    public int saveDeliveryAddress(HttpServletRequest request){
        DeliveryAddressService service = new DeliveryAddressServiceImpl();
        String contactName = request.getParameter("contactName");
        int contactSex = Integer.parseInt(request.getParameter("contactSex"));
        String contactTel = request.getParameter("contactTel");
        String address = request.getParameter("address");
        String userId = request.getParameter("userId");
        return service.saveDeliveryAddress(contactName, contactSex, contactTel, address, userId);
    }

    /**
     * 根据送货地址编号更新送货地址信息
     * @param request
     * @return
     */
    public int updateDeliveryAddress(HttpServletRequest request){
        DeliveryAddressService service = new DeliveryAddressServiceImpl();
        String contactName = request.getParameter("contactName");
        int contactSex = Integer.parseInt(request.getParameter("contactSex"));
        String contactTel = request.getParameter("contactTel");
        String address = request.getParameter("address");
        String userId = request.getParameter("userId");
        int daId =Integer.parseInt(request.getParameter("daId"));
        return service.updateDeliveryAddress(daId,contactName,contactSex,contactTel,address,userId);
    }

    /**
     * 根据送货地址编号删除一条记录
     * @param request
     * @return
     */
    public int removeDeliveryAddress(HttpServletRequest request){
        DeliveryAddressService service = new DeliveryAddressServiceImpl();
        int daId = Integer.parseInt(request.getParameter("daId"));
        return service.removeDeliveryAddress(daId);
    }



}
